package org.linlinjava.litemall.db.service;

import com.github.pagehelper.PageHelper;
import org.linlinjava.litemall.db.dao.LitemallMechanismCategoryMapper;
import org.linlinjava.litemall.db.domain.LitemallCategory;
import org.linlinjava.litemall.db.domain.LitemallCategoryExample;
import org.linlinjava.litemall.db.domain.LitemallMechanismCategory;
import org.linlinjava.litemall.db.domain.LitemallMechanismCategoryExample;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;

@Service
public class LitemallMechanismCategoryService {

    @Resource
    LitemallMechanismCategoryMapper mechanismCategoryMapper;

    public int add(LitemallMechanismCategory mechanismCategory){
        return mechanismCategoryMapper.insertSelective(mechanismCategory);
    }

    public List<LitemallMechanismCategory> querySelective(String id, String name, Integer page, Integer size, String sort, String order){
        LitemallMechanismCategoryExample mechanismCategoryExample = new LitemallMechanismCategoryExample();
        LitemallMechanismCategoryExample.Criteria criteria = mechanismCategoryExample.createCriteria();
        if(!StringUtils.isEmpty(id)){
            criteria.andIdEqualTo(Integer.valueOf(id));
        }
        if(!StringUtils.isEmpty(name)){
            criteria.andNameLike("%"+name+"%");
        }
        criteria.andDeletedEqualTo(false);
        PageHelper.startPage(page, size);
        return mechanismCategoryMapper.selectByExample(mechanismCategoryExample);
    }

    public int countSelective(String id, String name, Integer page, Integer size, String sort, String order) {
        LitemallMechanismCategoryExample mechanismCategoryExample = new LitemallMechanismCategoryExample();
        LitemallMechanismCategoryExample.Criteria criteria = mechanismCategoryExample.createCriteria();
        if(!StringUtils.isEmpty(id)){
            criteria.andIdEqualTo(Integer.valueOf(id));
        }
        if(!StringUtils.isEmpty(name)){
            criteria.andNameLike("%"+name+"%");
        }
        criteria.andDeletedEqualTo(false);

        return (int)mechanismCategoryMapper.countByExample(mechanismCategoryExample);
    }

    public void deleteById(Integer id) {
        mechanismCategoryMapper.logicalDeleteByPrimaryKey(id);
    }

    public void updateById(LitemallMechanismCategory mechanismCategory) {
        mechanismCategoryMapper.updateByPrimaryKeySelective(mechanismCategory);
    }

    public List<LitemallMechanismCategory> selectAll(){
        LitemallMechanismCategoryExample mechanismCategoryExample = new LitemallMechanismCategoryExample();
        LitemallMechanismCategoryExample.Criteria criteria = mechanismCategoryExample.createCriteria();
        criteria.andDeletedEqualTo(false);
        return mechanismCategoryMapper.selectByExample(mechanismCategoryExample);
    }
}
